<template>
  <div class="ai-writing-dialog">
    <!-- Main Dialog -->
    <el-dialog
      :visible.sync="dialogVisible"
      width="60%"
      @close="$emit('close')"
      :before-close="handleClose"
      custom-class="writing-dialog"
      :show-close="false"
    >
      <div class="header">
        <div class="title">配置AI写作导引</div>
        <div class="steps-wrapper">
          <el-steps :active="activeStep" align-center>
            <el-step :title="item" v-for="item in steps" :key="item"></el-step>
          </el-steps>
        </div>
      </div>
      <template v-if="isLast">
        <div class="null">
          <img src="@/assets/images/success.png" alt="" />
          <div class="null-text">已完成</div>
        </div>
        <div class="dialog-footer">
          <span></span>
          <el-button type="primary" @click="$emit('close')">关闭</el-button>
        </div>
      </template>
      <template v-else>
        <div class="content-section">
          <div class="world-setting">
            <div class="world-head">
              <div class="lf">
                <h3>{{ steps[activeStep] }}配置</h3>
                <p class="description">构建一个完整而有深度的世界观</p>
              </div>
              <span class="action" @click="showAllFullContent">
                {{
                  getDefaultLength() == getCurrentLength() ? "取消全选" : "全选"
                }}
              </span>
            </div>

            <div class="history-items" v-loading="loading">
              <div
                class="history-item"
                v-for="(item, index) in historyItems"
                :key="index"
              >
                <div class="item-header">
                  <span class="title"
                    >{{ item.name }}
                    <span class="num" v-if="getCurrentLength(item.prompts)">{{
                      getCurrentLength(item.prompts)
                    }}</span>
                  </span>
                  <span class="action" @click="showFullContent(item)">
                    {{
                      item.prompts.length == getCurrentLength(item.prompts)
                        ? "取消全选"
                        : "全选"
                    }}
                  </span>
                </div>
                <p class="sub-text">{{ item.desc }}</p>
                <div class="tags">
                  <div
                    class="tag"
                    :class="{ active: tag.checked }"
                    v-for="(tag, index) in item.prompts"
                    :key="index"
                    @click="tag.checked = !tag.checked"
                  >
                    {{ tag.title }}
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="dialog-footer">
          <div class="lf">
            <span>已选</span>
            <span class="num">{{ getCurrentLength() }}</span>
          </div>
          <div class="rg">
            <el-button @click="dialogVisible = false">退出</el-button>
            <el-button type="primary" @click="handleGenerate" class="aiWrite">
              <img src="@/assets/images/aiWrite.svg" alt="" /> AI生成</el-button
            >
            <el-button type="primary" @click="handleNext" v-if="isShowNext"
              >下一步</el-button
            >
          </div>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getTools, createGuide, executeGuide, nextGuide } from "@/api/tk";
let timer = null;
export default {
  name: "AIWritingDialog",
  data() {
    return {
      loading: true,
      steps: [
        "世界观",
        "构思大纲",
        "分章规划",
        "制定章纲",
        "正文写作",
        "润色打磨",
      ],
      isShowNext: false,
      dialogVisible: true,
      processingDialogVisible: false,
      activeStep: 0,
      processingProgress: 60,
      historyItems: [],
      taskId: "",
      stageId: "",
    };
  },
  computed: {
    isLast() {
      return this.isShowNext && this.activeStep == 5;
    },
  },
  created() {
    this.createGuideId();
  },
  methods: {
    //下一步
    handleNext() {
      nextGuide({ task_id: this.taskId }).then((res) => {
        this.taskId = res.id;
        this.activeStep = res.current_stage - 2;
        this.isShowNext = res.status == 5;
        this.getToolList();
      });
    },
    //创建AI写作导引任务接口 如果有就返回 没有就创建 taskId
    async createGuideId() {
      const id = this.$route.params.id;
      const res = await createGuide({ creation_id: Number(id) });
      this.taskId = res.task.id;
      this.activeStep = res.task.current_stage - 2;
      this.isShowNext = res.task.status == 5;
      this.getToolList();
    },
    //获取工具列表
    getToolList() {
      this.loading = true;
      getTools({ stage_seq: this.activeStep + 2 }).then((res) => {
        res.groups.forEach((item) => {
          item.prompts.forEach((prompt) => {
            prompt.checked = false;
          });
        });
        this.stageId = res.id;
        this.historyItems = res.groups;
        this.loading = false;
      });
    },
    //获取当前默认长度
    getDefaultLength() {
      return this.historyItems
        .map((x) => x.prompts.length)
        .reduce((a, b) => a + b, 0);
    },
    //获取当前选中长度
    getCurrentLength(prompts) {
      if (prompts) return prompts.filter((x) => x.checked).length;
      else
        return this.historyItems
          .filter((x) => x.prompts.filter((x) => x.checked).length > 0)
          .map((x) => x.prompts.filter((x) => x.checked).length)
          .reduce((a, b) => a + b, 0);
    },
    handleClose() {
      this.dialogVisible = false;
    },
    //ai生成
    async handleGenerate() {
      //转成需要的格式
      let groups = this.historyItems
        .filter((x) => x.prompts.filter((x) => x.checked).length > 0)
        .map((x) => {
          return {
            group_id: x.id,
            element_ids: x.prompts.filter((x) => x.checked).map((x) => x.name),
          };
        });
      if (!groups.length) {
        this.$message.warning("请选择至少一个元素");
        return;
      }
      //参数
      const params = {
        task_id: this.taskId,
        elements: {
          stage_id: this.stageId,
          groups,
        },
      };
      executeGuide(params).then((res) => {
        // this.getTaskStatus()
        this.dialogVisible = false;
        // this.processingDialogVisible = true
        this.$emit("getTaskId", this.taskId);
      });
    },
    //全部全选
    showAllFullContent() {
      //取消全选
      if (this.getCurrentLength() == this.getDefaultLength()) {
        this.historyItems.forEach((item) => {
          item.prompts.forEach((prompt) => {
            prompt.checked = false;
          });
        });
      } else {
        this.historyItems.forEach((item) => {
          item.prompts.forEach((prompt) => {
            prompt.checked = true;
          });
        });
      }
    },
    //局部全选
    showFullContent(item) {
      //取消全选
      if (item.prompts.filter((x) => x.checked).length == item.prompts.length) {
        item.prompts.forEach((prompt) => {
          prompt.checked = false;
        });
        //全选
      } else {
        item.prompts.forEach((prompt) => {
          prompt.checked = true;
        });
      }

      // Implementation for showing full content
    },
  },
};
</script>

<style lang="scss" scoped>
.null {
  padding: 200px 0;
  text-align: center;
  img {
    width: 100px;
    height: 100px;
  }
  .null-text {
    color: #00011b;
    font-family: "PingFang SC";
    font-size: 24px;
    font-style: normal;
    font-weight: 400;
    line-height: normal;
  }
}
.ai-writing-dialog {
  .writing-dialog {
    .header {
      background: #f2f8ff;

      .title {
        font-weight: 500;
        font-size: 20px;
        color: #00011b;
        text-align: center;
        padding: 24px 0;
      }
    }

    .steps-wrapper {
      padding: 20px 0;
    }

    .content-section {
      background: #f2f8ff;

      .world-setting {
        border-radius: 20px 20px 0px 0px;
        background: #fff;
        padding: 24px 32px 0 32px;
        .world-head {
          display: flex;
          justify-content: space-between;
          .action {
            color: #165deb;
            cursor: pointer;
            font-size: 14px;
          }
        }
        h3 {
          font-weight: 500;
          font-size: 16px;
          color: #00011b;
          line-height: 19px;
          margin: 0;
        }

        .description {
          font-weight: 400;
          font-size: 14px;
          color: rgba(0, 1, 27, 0.5);
          line-height: 16px;
          margin-top: 4px;
        }

        .history-items {
          margin-top: 24px;

          .history-item {
            padding-bottom: 20px;
            border-left: 1px solid rgba(0, 0, 0, 0.08);
            padding-left: 15px;
            position: relative;

            &::after {
              content: "";
              width: 2px;
              height: 41px;
              background: #165deb;
              border-radius: 0px 0px 0px 0px;
              opacity: 0.8;
              display: block;
              position: absolute;
              left: -1px;
              top: 0;
            }

            .item-header {
              display: flex;
              justify-content: space-between;
              align-items: center;
              margin-bottom: 8px;

              .title {
                font-weight: 500;

                .num {
                  padding: 0 7px;
                  height: 18px;
                  background: #ec1111;
                  border-radius: 41px 41px 41px 41px;
                  font-weight: 500;
                  font-size: 12px;
                  color: #ffffff;
                  line-height: 18px;
                  margin-left: 8px;
                }
              }

              .action {
                color: #165deb;
                cursor: pointer;
                font-size: 14px;
              }
            }

            .sub-text {
              color: #666;
              font-size: 12px;
              margin: 8px 0;
            }

            .tags {
              display: flex;
              flex-wrap: wrap;
              gap: 8px;

              .tag {
                width: 125px;
                height: 37px;
                text-align: center;
                background: #f2f4f8;
                border-radius: 4px 4px 4px 4px;
                border: 1px solid rgba(0, 0, 0, 0.08);
                font-weight: 400;
                font-size: 12px;
                color: #00011b;
                cursor: pointer;
                box-sizing: border-box;
                padding: 8px 4px;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
              }

              .tag.active {
                color: #165deb;
                border: 1px solid #165deb;
              }
            }
          }
        }
      }
    }

    .dialog-footer {
      display: flex;
      justify-content: space-between;
      padding: 0 32px;

      .lf {
        .num {
          padding: 0 7px;
          height: 18px;
          background: #ec1111;
          border-radius: 41px 41px 41px 41px;
          font-weight: 500;
          font-size: 12px;
          color: #ffffff;
          line-height: 18px;
          margin-left: 8px;
        }
      }

      .aiWrite {
        padding-left: 40px;
        position: relative;

        img {
          width: 18px;
          height: 18px;
          position: absolute;
          left: 15px;
          top: 50%;
          margin-top: -9px;
        }
      }
    }
  }
}

//弹框
::v-deep .writing-dialog {
  border-radius: 12px 12px 12px 12px;
  overflow: hidden;

  .el-dialog__header {
    display: none;
  }

  .el-dialog__body {
    padding: 0;
    background: transparent;
  }
}

//进度条
::v-deep .el-step {
  //选中
  .el-step__head.is-process,
  .el-step__head.is-finish {
    border: 0;

    .el-step__icon.is-text {
      background: #165deb;
      border: 0;
      font-weight: 500;
      font-size: 13px;
      color: #ffffff;
    }
  }
  .el-step__head.is-finish {
    .el-step__line {
      height: 2px;
      background: #165deb;
    }
  }

  .el-step__title.is-process {
    font-weight: 400;
    font-size: 14px;
    color: #165deb;
  }

  //未选中
  .el-step__line {
    background: #c4d6fb;
    height: 1px;
  }

  .el-step__icon.is-text {
    width: 24px;
    height: 24px;
    background: #ffffff;
    border: 0;
    font-weight: 500;
    font-size: 13px;
    color: rgba(0, 1, 27, 0.3);
  }

  .el-step__title.is-wait {
    font-weight: 400;
    font-size: 14px;
    color: rgba(0, 1, 27, 0.5);
  }
}
</style>
